/*
 * User: user
 * Date: 28/06/2008
 * Time: 14:41:31
 */
package org.conann.instrument;

import javassist.CannotCompileException;
import javassist.ClassPool;
import javassist.CtClass;
import org.conann.exceptions.WebBeansSystemException;
import org.conann.exceptions.WebBransImlementationBugException;

import javax.webbeans.inwork.MissingDocumentation;
import javax.webbeans.inwork.MissingTests;
import java.io.IOException;
import java.io.InputStream;
import java.lang.annotation.Annotation;

public class AnnotationsFactory {

    @MissingTests
    @MissingDocumentation
    public <T extends Annotation> T createAnnotationOfType(Class<T> annotationType, Object... params) {
        ClassPool pool = ClassPool.getDefault();
        CtClass ctClass;
        try {
            InputStream inputStream = null;
            ctClass = pool.makeClass(inputStream);
        } catch (IOException e) {
            throw new WebBeansSystemException(e, "Can't create implementation of %s. Maybe next time dude.", annotationType.getName());
        }
        Class<T> annotationClass;
        try {
            annotationClass = ctClass.toClass();
        } catch (CannotCompileException e) {
            throw new WebBransImlementationBugException(e, "Annotation class implementing %s does not compile." + annotationType.getName());
        }

        try {
            return annotationClass.newInstance();
        } catch (InstantiationException e) {
            throw new WebBransImlementationBugException(e, "Cannot instantiate implementation of %s.", annotationType.getName());
        } catch (IllegalAccessException e) {
            throw new WebBransImlementationBugException(e, "Cannot instantiate implementation of %s.", annotationType.getName());
        }
    }
}